package com.bite.springblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bite.springblog.common.exception.BlogException;
import com.bite.springblog.common.utils.BeanTransUtils;
import com.bite.springblog.mapper.BlogInfoMapper;
import com.bite.springblog.pojo.daoobject.BlogInfo;
import com.bite.springblog.pojo.request.BlogInfoRequest;
import com.bite.springblog.pojo.request.UpdateBlogRequest;
import com.bite.springblog.pojo.response.BlogResponse;
import com.bite.springblog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogResponse> getBolgList() {
        LambdaQueryWrapper<BlogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogInfo::getDeleteFlag,0);
        List<BlogInfo> blogInfoList = blogInfoMapper.selectList(queryWrapper);
        //返回类型属性转换
        List<BlogResponse> dlist = blogInfoList.stream()
                .map(blogInfo -> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());
        return dlist;
    }

    @Override
    public BlogResponse getBlogDetail(String blogId) {
        LambdaQueryWrapper<BlogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogInfo::getDeleteFlag,0);
        queryWrapper.eq(BlogInfo::getId,blogId);
        BlogInfo blogInfo = blogInfoMapper.selectOne(queryWrapper);
        BlogResponse blogDetail = BeanTransUtils.trans(blogInfo);
        return blogDetail;
    }

    @Override
    public boolean addBolg(BlogInfoRequest blogInfoRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(blogInfoRequest,blogInfo);
        try {
            blogInfoMapper.insert(blogInfo);
            return true;
        }catch (Exception e){
            log.error("数据库错误，e{}",e);
            throw new BlogException("内部异常，联系管理员");
        }
    }

    @Override
    public boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest,blogInfo);
        try {
            Integer ret = blogInfoMapper.updateById(blogInfo);
            return ret == 1;
        }catch (Exception e){
            log.error("更新博客错误，e{}",e);
            throw new BlogException("内部异常，联系管理员");
        }
    }

    @Override
    public boolean deleteBlog(String blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(Integer.valueOf(blogId));
        blogInfo.setDeleteFlag(1);
        try {
            Integer ret = blogInfoMapper.updateById(blogInfo);
            return ret == 1;
        }catch (Exception e){
            log.error("删除博客错误，e{}",e);
            throw new BlogException("内部异常，联系管理员");
        }
    }
}





